package sparkExamples.exerciseDemo

import org.apache.spark.{SparkConf, SparkContext}

object ExerciseDemo02 {

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName("ExerciseDemo").setMaster("local[*]")
    val sparkContext = new SparkContext(conf)

    val txtRDD = sparkContext.textFile("src\\main\\scala\\data\\exerciseData.txt")

    /**
     * 5、一共有多个男生参加考试？
     * 6、一共有多少个女生参加考试？
     */
    val name_sex = txtRDD.map {
      line => {
        val strings = line.split(" ")
        (strings(1), strings(3))
      }
    }
    val name_sex1 = name_sex.distinct()

    val result = name_sex1.map {
      line => {
        if (line._2 == "男") {
          ("男", 1)
        } else {
          ("女", 1)
        }
      }
    }.reduceByKey(_ + _)

    result.collect().foreach(println)

    sparkContext.stop()
  }

}
